package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.ui.activity.map.LocationActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class SwitchStatusActivityNew extends AppActivity {
    private SettingBar settingAction;
    public static final int TYPE_POWER = 0;
    public static final int TYPE_CURTAIN = 1;

    private TextView tvCondition;
    private RadioGroup whenRadioGroup;
    private int type;
    private String deviceName;
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageDeviceCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_switch_status_new;
    }

    @Override
    protected void initView() {
        mLinkageCondition =  getSerializable("condition");
        type = getInt("type");
        switch (type){
            case TYPE_POWER:
                setTitle(R.string.switch_status);
                break;
            case TYPE_CURTAIN:
                setTitle(R.string.Linkage_Device_CurtainState);
                break;
        }
        settingAction = findViewById(R.id.setting_action);
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        setOnClickListener(settingAction);

//        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();
        if (mLinkageCondition == null) {
            deviceName = getString("device_name");
            mLinkageCondition = new LinkageNewBean.LinkageDeviceCondition();
            mLinkageCondition.setConditionTypeId(8);
            mLinkageCondition.setAreaTypeId(1);
            mLinkageCondition.setCompareTypeId(2);
            mLinkageCondition.setDeviceSerialNo(getString("device_serial_no"));
            mLinkageCondition.setAreaName(getString("floor_room_name"));
            mLinkageCondition.setAreaCode(getString("room_serial_no"));
            mLinkageCondition.setActionTypeId(1);
            mLinkageCondition.setOperatorTypeId(1);
            switch (type){
                case TYPE_POWER:
                    mLinkageCondition.setBeginValue(1);
                    break;
                case TYPE_CURTAIN:
                    mLinkageCondition.setBeginValue(2);
                    break;
            }
            mLinkageCondition.setIcon(getString("device_image"));
            mLinkageCondition.setDescription(deviceName);
            switch (type){
                case TYPE_POWER:
                    mLinkageCondition.setStateName(LLDeviceUtil.POWER);
                    break;
                case TYPE_CURTAIN:
                    mLinkageCondition.setStateName(LLDeviceUtil.STROKE_STATE);
                    break;
            }
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }else{
            DeviceBean deviceBean = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(mLinkageCondition.getDeviceSerialNo());
            if(deviceBean != null){
                deviceName = deviceBean.getName();
            }
        }
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_on:
                        switch (type){
                            case TYPE_POWER:
                                mLinkageCondition.setBeginValue(1);
                                break;
                            case TYPE_CURTAIN:
                                mLinkageCondition.setBeginValue(2);
                                break;
                        }
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.switch_on)));
                        break;
                    case R.id.type_off:
                        switch (type){
                            case TYPE_POWER:
                                mLinkageCondition.setBeginValue(0);
                                break;
                            case TYPE_CURTAIN:
                                mLinkageCondition.setBeginValue(1);
                                break;
                        }
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.switch_off)));
                        break;
                }
            }
        });
        updateView();
    }

    private void updateView() {
        switch (type){
            case TYPE_POWER:
                switch ((int) mLinkageCondition.getBeginValue()) {
                    case 1:
                        ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                        break;
                    case 0:
                        ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                        break;
                }
                break;
            case TYPE_CURTAIN:
                switch ((int) mLinkageCondition.getBeginValue()) {
                    case 2:
                        ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                        break;
                    case 1:
                        ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                        break;
                }
                break;
        }
        settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
    }

    @Override
    public void onRightClick(View view) {
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }

        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_action:
                showBottomListDialog();
                break;
        }
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            mBottomActionDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("变为");
            data.add("处于");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mLinkageCondition.setActionTypeId(position + 1);
                    settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
                    mBottomActionDialog.dismiss();
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }
}